winsafe\gui\native_controls/
tab_items.rs

1use crate::co;
2use crate::decl::*;
3use crate::gui::*;
4use crate::msg::*;
5use crate::prelude::*;
6
7/// Exposes the methods of a [`Tab`](crate::gui::Tab) control.
8///
9/// You cannot directly instantiate this object, it is created internally by the
10/// control.
11pub struct TabItems<'a> {
12	owner: &'a Tab,
13}
14
15impl<'a> TabItems<'a> {
16	#[must_use]
17	pub(in crate::gui) const fn new(owner: &'a Tab) -> Self {
18		Self { owner }
19	}
20
21	/// Manually appends a new tab by sending a
22	/// [`tcm::InsertItem`](crate::msg::tcm::InsertItem) message, and returns
23	/// the newly added item.
24	///
25	/// # Safety
26	///
27	/// By adding a tab item manually, you are responsible for all the message
28	/// handling. Prefer adding items automatically by filling the
29	/// [`TabOpts::items`](crate::gui::TabOpts::items) member when calling the
30	/// [`Tab::new`](crate::gui::Tab::new) function.
31	pub unsafe fn add(&self, title: &str) -> TabItem<'a> {
32		let mut wtitle = WString::from_str(title);
33		let mut tci = TCITEM::default();
34		tci.mask = co::TCIF::TEXT;
35		tci.set_pszText(Some(&mut wtitle));
36
37		let idx = unsafe {
38			self.owner.hwnd().SendMessage(tcm::InsertItem {
39				index: 0x0fff_ffff, // insert as the last item
40				item: &tci,
41			})
42		}
43		.unwrap();
44		self.get(idx)
45	}
46
47	/// Retrieves the total number of items by sending an
48	/// [`tcm::GetItemCount`](crate::msg::tcm::GetItemCount) message.
49	#[must_use]
50	pub fn count(&self) -> SysResult<u32> {
51		unsafe { self.owner.hwnd().SendMessage(tcm::GetItemCount {}) }
52	}
53
54	/// Deletes all items by sending a
55	/// [`tcm::DeleteAllItems`](crate::msg::tcm::DeleteAllItems) message.
56	///
57	/// # Safety
58	///
59	/// If you delete a tab automatically created, which has a container window
60	/// attached to it, the rendering will be out-of-order.
61	pub unsafe fn delete_all(&self) -> SysResult<()> {
62		unsafe { self.owner.hwnd().SendMessage(tcm::DeleteAllItems {}) }
63	}
64
65	/// Retrieves the item at the given zero-based position.
66	///
67	/// **Note:** This method is cheap – even if `index` is beyond the range of
68	/// existing items, an object will still be returned. However, operations
69	/// upon this object will produce no effect.
70	#[must_use]
71	pub const fn get(&self, index: u32) -> TabItem<'a> {
72		TabItem::new(self.owner, index)
73	}
74
75	/// Returns the focused item by sending a
76	/// [`tcm::GetCurFocus`](crate::msg::tcm::GetCurFocus) message.
77	#[must_use]
78	pub fn focused(&self) -> Option<TabItem<'a>> {
79		unsafe { self.owner.hwnd().SendMessage(tcm::GetCurFocus {}) }.map(|i| self.get(i))
80	}
81
82	/// Returns the selected item by sending a
83	/// [`tcm::GetCurSel`](crate::msg::tcm::GetCurSel) message.
84	#[must_use]
85	pub fn selected(&self) -> Option<TabItem<'a>> {
86		unsafe { self.owner.hwnd().SendMessage(tcm::GetCurSel {}) }.map(|i| self.get(i))
87	}
88}